<?php
/**
 * Factory to create a FactoryMethodValueConverter.
 *
 * @author  Stephan Schmidt <stephan.schmidt@schlund.de>
 * @author  Frank Kleine <frank.kleine@schlund.de>
 */
XJConfLoader::load('converters::factories::ValueConverterFactory',
                   'converters::FactoryMethodValueConverter'
);
/**
 * Factory to create an FactoryMethodValueConverter.
 * 
 * @package     XJConf
 * @subpackage  converters
 */
class FactoryMethodValueConverterFactory implements ValueConverterFactory
{
    /**
     * Decides whether the FactoryMethodValueConverter is responsible for the given Definition.
     *
     * @param   Definition  $def
     * @return  boolean     true if is responsible, else false
     */
    public function isResponsible(Definition $def)
    {
        if (class_exists($def->getType()) == false) {
            return false;
        }
        
        return $def->hasChildDefinition('FactoryMethodDefinition');
    }
    
    /**
     * creates an instance of the FactoryMethodValueConverter
     *
     * @param   Definition                   $def
     * @return  FactoryMethodValueConverter
     */
    public function createValueConverter(Definition $def)
    {
        $factoryMethod = $def->getChildDefinition('FactoryMethodDefinition');
        if (null == $factoryMethod) {
            return null;
        }
        
        $converter = new FactoryMethodValueConverter($def->getType(), $factoryMethod->getName());
        return $converter;
    }
}
?>